<template>
<div class="app-container">
    <!-- 课程信息表单 -->
    <el-form label-width="120px">
        <!-- 课程标题 -->
        <el-form-item label="课程标题">
            <el-input v-model="courseInfo.title" 
            placeholder="示例：机器学习项目：从基础到搭建项目视频，专业名称注意大小写" />
        </el-form-item>

        <!-- 所属分类 -->
        <el-form-item label="课程分类">
            <el-select 
            v-model="courseInfo.subjectParentId" 
            placeholder="一级分类"
            @change="subjectLevelOneChanged">
                <el-option 
                    v-for="subject in subjectOneList"
                    :key="subject.id"
                    :label="subject.title"
                    :value="subject.id" />
            </el-select>

            <el-select 
            v-model="courseInfo.subjectId" 
            placeholder="二级分类">
                <el-option 
                    v-for="subject in subjectTwoList"
                    :key="subject.id"
                    :label="subject.title"
                    :value="subject.id" />
            </el-select>
        </el-form-item>

        <!-- 课程讲师 -->
        <el-form-item label="课程讲师">
            <el-select v-model="courseInfo.teacherId" placeholder="请选择">
                <el-option 
                    v-for="teacher in teacherList"
                    :key="teacher.id"
                    :label="teacher.name"
                    :value="teacher.id" />
            </el-select>
        </el-form-item>

        <!-- 总课时 -->
        <el-form-item label="总课时">
            <el-input-number 
            :min="0" 
            v-model="courseInfo.lessonNum" 
            controls-position="right"
            placeholder="请填写课程的总课时" />
        </el-form-item>

        <!-- 课程简介 -->
        <el-form-item label="课程简介">
            <tinymce
            :height="300"
            v-model="courseInfo.description" />
        </el-form-item>

        <!-- 课程封面 -->
        <el-form-item label="课程封面">
            <el-upload
                :show-file-list="false"
                :on-success="handleAvatarSuccess"
                :before-upload="beforeAvatarUpload"
                :action="BASE_API + '/ossService/upload'"
                class="avatar-uploader">
                <img :src="courseInfo.cover" :width="400" />
            </el-upload>
        </el-form-item>

        <!-- 课程价格 -->
        <el-form-item label="课程价格">
            <el-input-number 
            :min="0" 
            v-model="courseInfo.price" 
            controls-position="right"
            placeholder="" />
        </el-form-item>
    </el-form>

    <div style="text-align:center">
        <el-button :disabled="saveBtnDisabled" type="success" 
        @click="saveAndNext()">保存并下一步</el-button>
    </div>
</div>
</template>
<script>
import course from '@/api/teacher/course'
import subject from '@/api/teacher/subject'
import Tinymce from '@/components/Tinymce'//引入组件
export default {
    components:{ Tinymce },
    data() {
        return {
            saveBtnDisabled:false,
            courseInfo:{
                title:'',
                subjectId:'',//二级分类ID
                subjectParentId:'',//一级分类ID
                teacherId:'',
                lessonNum:0,
                description:'',
                cover:'/static/default.png',
                price:0
            },
            BASE_API:process.env.BASE_API,
            teacherList:[],//封装所有讲师
            subjectOneList:[],
            subjectTwoList:[]
        }
    },
    created() {
         // courseId来自父组件
        if(this.$parent.courseId){
            this.getInfo()
        }else{
             //初始化所有讲师
            this.getListTeacher()
            //初始化一级分类
            this.getOneSubject()
        }
    },
    methods: {
        //保存并下一步
        saveAndNext(){
            this.saveBtnDisabled = true
            if(!this.$parent.courseId){
                this.addCourse()
            }else{
                this.updateCourse()
            }
        },
        // 根据课程id进行数据回显
        getInfo(){
            course.getCourseInfoById(this.$parent.courseId)
            .then( res => {
                this.courseInfo = res.data.courseInfoVo
                // 1 查询所有的分类，包含一级和二级
                 subject.getSubjectList()
                    .then(res => {
                        // 2 获取所有一级分类
                        this.subjectOneList = res.data.list
                        // 3 遍历一级分类
                        for(var i=0;i<this.subjectOneList.length;i++){
                            // 获取每个一级分类
                            var oneSubject = this.subjectOneList[i]
                            // 比较当前courseInfo里面的一级分类和所有的一级分类id
                            if(this.courseInfo.subjectParentId == oneSubject.id){
                                // 获取一级分类所有的二级分类
                                this.subjectTwoList = oneSubject.children
                                // for(var j=0;j<this.subjectTwoList.length;j++){
                                //      console.log(this.subjectTwoList[j].id+"--"+this.subjectTwoList[j].title)
                                // }
                            }
                        }
                    })
                //初始化所有讲师
                this.getListTeacher()
            })
        },
        // 上传封面之前调用的方法
        beforeAvatarUpload(file){
            const isJPG = file.type === 'image/jpeg'
            const isLt2M = file.size / 1024 / 1024 < 2

            if(!isJPG){
                this.$message.error('上传头像图片只能是JPG格式！')
            }

            if(!isLt2M){
                this.$message.error('上传头像图片大小不能超过 2MB！')
            }

            return isJPG && isLt2M
        },
        // 上传封面成功调用的方法
        handleAvatarSuccess(res,file){
            this.courseInfo.cover = res.data.url
        },
        //点击某个一级分类，触发change，显示对应的二级分类
        subjectLevelOneChanged(value){
            //value就是一级分类ID的值
            //遍历所有的分类，包含一级和二级
            for(var i=0;i<this.subjectOneList.length;i++){
                var oneSubject = this.subjectOneList[i]
                // 判断：所有的一级分类ID和点击一级分类ID是否一样
                if(value === oneSubject.id){
                    this.subjectTwoList = oneSubject.children
                    //清空二级分类
                    this.courseInfo.subjectId = ''
                }
            }
        },
        getOneSubject(){
            subject.getSubjectList()
            .then(res => {
                this.subjectOneList = res.data.list
            })
        },
        //获取所有讲师数据
        getListTeacher(){
            course.getTeacherList()
            .then(res => {
                this.teacherList = res.data.items
            })
        },
        //添加课程
        addCourse(){
            course.addCourseInfo(this.courseInfo)
            .then(res => {
                this.$message({
                    type:'success',
                    message:'添加课程信息成功！'
                })
            //下一步
            this.$parent.active = 1
            this.$parent.courseId = res.data.courseId
            })
        },
        //修改课程
        updateCourse(){
            course.updateCourseInfo(this.courseInfo)
            .then(res => {
                this.$message({
                    type:'success',
                    message:'修改课程信息成功！'
                })
                console.log(this.courseInfo.subjectId)
            //下一步
            this.$parent.active = 1
            })
        }
    }
}
</script>